package com.example.docking.service.impl;

import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.MaterialGroupData;
import com.example.docking.service.MaterialGroupService;
import com.example.docking.util.Constants;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MaterialGroupServiceImpl implements MaterialGroupService {
    @Override
    public String getMaterialGrouptId(DataServiceImpl service, String materType) throws Exception {
        Gson gson = new Gson();
        // 设置分页参数
        int page = 1; // 初始页码
        int pageSize = 10; // 每页记录数
        HashMap<String, String> map = new HashMap<>();
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        String group_id = "";

        ApiResult DataResult = service.SelectDataDetail(Constants.MATERIAL_GROUP, map, "");
        Type MaterialGroupDataType = new TypeToken<ResponseData<GenericDataItem<MaterialGroupData>>>() {
        }.getType();
        ResponseData<GenericDataItem<MaterialGroupData>> responseGroupData = gson.fromJson(DataResult.getBody(), MaterialGroupDataType);
        if (responseGroupData.getErrcode() == 0) {
            GenericDataItem<MaterialGroupData> genericDataItemDepart = responseGroupData.getData();

            // 解析分页信息
            String count = genericDataItemDepart.getCount();
            int countNum = Integer.valueOf(count);
            int Pagesum = pageSize;
            List<MaterialGroupData> GroupData = genericDataItemDepart.getRows();

            List<MaterialGroupData> allData = new ArrayList<>(GroupData);
            // 如果不是最后一页，继续获取下一页数据
            while (Pagesum < countNum) {
                page++;
                map.put("page", String.valueOf(page));
                DataResult = service.SelectDataDetail(Constants.MATERIAL_GROUP, map, "");
                responseGroupData = gson.fromJson(DataResult.getBody(), MaterialGroupDataType);
                if (responseGroupData.getErrcode() == 0) {
                    genericDataItemDepart = responseGroupData.getData();
                    GroupData = genericDataItemDepart.getRows();
                    allData.addAll(GroupData);
                    Pagesum = pageSize * page;
                } else {
                    throw new RuntimeException(responseGroupData.getErrcode() + ":" + responseGroupData.getDescription());
                }
            }

            boolean departmentFound = false;
            for (MaterialGroupData departmentData : allData) {
                if (departmentData.getName().equals(materType)) {
                    group_id = departmentData.getId();
                    departmentFound = true;
                    break;
                }
            }
            if (!departmentFound) {
                // 这里可以根据业务需求添加未找到部门时的处理逻辑，比如抛异常等
                // 目前简单返回空字符串
                group_id = "";

            }
        } else {
            throw new RuntimeException(responseGroupData.getErrcode() + ":" + responseGroupData.getDescription());
        }

        return group_id;
    }

}
